package fila;

import java.lang.reflect.Array;

/**
 * Implementacao de filas para objetos genericos
 */
public class Fila<T> {
	private T[] arr;
	private int fim;

	@SuppressWarnings("unchecked")
	public Fila(Class<T> c, int max) {
		arr = (T[])Array.newInstance(c, max);
		fim = -1;
	}
	
	public int getLenght() {
		return fim + 1;
	}

	public T getFirst() {
		if(fim == -1) return null;

		T aux = arr[0];
		for(int i = 1; i <= fim; i++) {
			arr[i - 1] = arr[i];
		}
		fim--;

		return aux;
	}

	public boolean add(T t) {
		if(fim == arr.length) return false;

		arr[++fim] = t;
		return true;
	}
	
	@Override
	public String toString() {
		if(fim == -1) return "";
		
		StringBuilder sb = new StringBuilder();
		sb.append('(');
		for(int i = 0; i <= fim; i++) {
			sb.append(arr[i].toString());
			if(i != fim) sb.append(", ");
		}
		sb.append(')');
		return sb.toString();
	}
}
